#include "ModuleManagerView.h"
#include <QGridLayout>
#include <QToolButton>
#include <QPushButton>
#include <QLabel>
#include <QDrag>
#include <QMouseEvent>
#include <QMimeData>

ModuleManagerView::ModuleManagerView(QWidget *parent)
    : QWidget(parent),m_itemType(-1)
{
    ui.setupUi(this);
    createToolBox();
    setAcceptDrops(true);
}

ModuleManagerView::~ModuleManagerView()
{

}

void ModuleManagerView::createToolBox()
{

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(createCellWidget(tr("Gain"), ModuleItem::Gain), 0, 0);
    layout->addWidget(createCellWidget(tr("Sum"), ModuleItem::Sum),0, 1);
    layout->addWidget(createCellWidget(tr("Mult"), ModuleItem::Mult), 1, 0);
    layout->addWidget(createCellWidget(tr("Sine"), ModuleItem::Sine), 1, 1);
    layout->addWidget(createCellWidget(tr("Cons"), ModuleItem::Cons), 2, 0);
    layout->addWidget(createCellWidget(tr("Disp"), ModuleItem::Disp), 2, 1);
    layout->addWidget(createCellWidget(tr("Sampler"), ModuleItem::Sampler),3,0);

    layout->setRowStretch(4, 8);
    layout->setColumnStretch(2, 10);

    QWidget *itemWidget = new QWidget;
    itemWidget->setLayout(layout);

    m_toolBox = new QToolBox;
    m_toolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored));
    m_toolBox->setMinimumWidth(itemWidget->sizeHint().width());
    m_toolBox->addItem(itemWidget, "Sub-Function Module");
    //toolBox->addItem(backgroundWidget, tr("Backgrounds"));
    QHBoxLayout *hlayout = new QHBoxLayout(this);
    hlayout->addWidget(m_toolBox);
}

QWidget *ModuleManagerView::createCellWidget(const QString &text, ModuleItem::DiagramType type)
{
    QPushButton *button = new QPushButton;
    button->setText(text);
    button->setFont(QFont("Microsoft YaHei",11));
    button->setFixedSize(QSize(100, 60));
    button->setProperty("ItemType",(int)type);
    connect(button,&QPushButton::pressed,this,&ModuleManagerView::onButtonPress);
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(button, 0, 0, Qt::AlignHCenter);
    layout->addWidget(new QLabel(ModuleItem::getText(type)), 1, 0, Qt::AlignCenter);

    QWidget *widget = new QWidget;
    widget->setLayout(layout);

    return widget;
}
void ModuleManagerView::onButtonPress()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender());
    if(button){
        m_itemType = button->property("ItemType").toInt();
    }
}
void ModuleManagerView::mousePressEvent(QMouseEvent *event)
{
	if (event->buttons() & Qt::LeftButton)
	{
		
	}
    return QWidget::mousePressEvent(event);
}
 
void ModuleManagerView::mouseMoveEvent(QMouseEvent *event)
{
	if (event->buttons() == Qt::LeftButton && m_itemType != -1)
	{		    
			QDrag *drag = new QDrag(this);
			QMimeData *data = new QMimeData;
            data->setText(QString::number(m_itemType));
			drag->setMimeData(data);
            drag->setPixmap(ModuleItem::image((ModuleItem::DiagramType)m_itemType));
			drag->exec(Qt::IgnoreAction | Qt::MoveAction | Qt::CopyAction, Qt::MoveAction);
		    m_itemType = -1;
            delete drag;
	}
    return QWidget::mouseMoveEvent(event);
}
